
import { _decorator, Component, Label, Node } from 'cc';
import EvtManger from '../tools/EvtManger';
import { AppControll, EventName, PopView } from '../manager/AppControll';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { GameData } from '../system/GameData';
import { SoundManager } from '../manager/SoundManger';
import { DataHelper } from '../system/DataHelper';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = TemporaryGift
 * DateTime = Fri May 26 2023 16:18:50 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = TemporaryGift.ts
 * FileBasenameNoExtension = TemporaryGift
 * URL = db://assets/script/pop/TemporaryGift.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

export enum GiftType {
    gold = 1,
    tripleCoins = 2,
    tripleDamage = 3,
    default = 0,
}

@ccclass('TemporaryGift')
export class TemporaryGift extends Component {
    @property(Node)
    mNdBody: Node = null;
    @property(Node)
    mNdGift: Node = null;

    private mGiftType: GiftType = GiftType.default;
    private mIsLock = false;
    private mNdGold = null;
    private mNdTripleCoins = null;
    private mNdtripleDamage = null;

    start() {
        GameData.isPause = true;
        this.mNdGold = this.mNdGift.getChildByName("aPileMoney");
        this.mNdTripleCoins = this.mNdGift.getChildByName("3xGold");
        this.mNdtripleDamage = this.mNdGift.getChildByName("3xAtk");
        this.mNdGold.getChildByName("gou").active = false;
        this.mNdTripleCoins.getChildByName("gou").active = false;
        this.mNdtripleDamage.getChildByName("gou").active = false;
        this.mNdGold.getChildByName("Label").getComponent(Label).string = DataHelper.GetMonsterRefreshByAuto().LimitedTimeAwardGold;
        const randomNum = Math.floor(Math.random() * 3) + 1;
        this.mGiftType = randomNum as GiftType;
        this.RegisterEvents();
        this.InitSelectGift();
    }

    RegisterEvents() {
    }

    InitSelectGift() {
        if (this.mGiftType == GiftType.default) return;
        let ndGold = this.mNdGift.getChildByName("aPileMoney");
        let ndTripleCoins = this.mNdGift.getChildByName("3xGold");
        let ndtripleDamage = this.mNdGift.getChildByName("3xAtk");
        ndGold.getChildByName("gou").active = false;
        ndGold.getChildByName("fx_rotate_line_small").active = false;

        ndTripleCoins.getChildByName("gou").active = false;
        ndTripleCoins.getChildByName("fx_rotate_line_small").active = false;

        ndtripleDamage.getChildByName("gou").active = false;
        ndtripleDamage.getChildByName("fx_rotate_line_small").active = false;

        let nd = null;
        switch (this.mGiftType) {
            case GiftType.gold:
                nd = ndGold;
                break;
            case GiftType.tripleCoins:
                nd = ndTripleCoins;
                break;
            case GiftType.tripleDamage:
                nd = ndtripleDamage;
                break;
        }
        nd.getChildByName("gou").active = true;
        nd.getChildByName("fx_rotate_line_small").active = true;
    }

    OnBtnConfirm() {
        AppControll.ShelvedTime = 0;
        if (this.mIsLock) return;

        this.scheduleOnce(() => {
            this.mIsLock = false;
        }, 1);
        EvtManger.do(EventName.receiveGift, this.mGiftType);
        GameData.isPause = false;
        this.node.active = false
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
    }

    OnBtnCancel() {
        AppControll.ShelvedTime = 0;
        if (this.mIsLock) return;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        GameData.isPause = false;
        this.node.active = false;
    }

    OnGetAll() {
        AppControll.ShelvedTime = 0;
        if (this.mIsLock) return;
        this.mIsLock = true;
        AppControll.Platform.ShowRewardVideoAd(this, (bool: Boolean, str: string) => {
            this.mIsLock = false;
            if (!bool) {
                PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                    node.getComponent(SystemMessage).InitContent(str);
                })
                return;
            }
            AppControll.Platform.SendBuryPoint(100008);
            EvtManger.do(EventName.receiveGift, GiftType.gold);
            EvtManger.do(EventName.receiveGift, GiftType.tripleCoins);
            EvtManger.do(EventName.receiveGift, GiftType.tripleDamage);
            GameData.isPause = false;
            this.node.active = false
            SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        })
    }

}
